package com.yc.cloud.admin.controller;

import com.yc.cloud.admin.dto.request.ProductCreateParam;
import com.yc.cloud.admin.dto.request.ProductUpdateParam;
import com.yc.cloud.admin.service.ProductService;
import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.basic.dto.BasePageReqDTO;
import com.yc.cloud.common.orm.CommonPage;
import com.yc.cloud.entity.Product;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import lombok.val;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 产品
 *
 * @author Yancy
 * @date 2024-01-21
 */
@RestController
@RequestMapping("/product")
@Api(value = "产品", tags = "产品接口")
public class ProductController {

    @Resource
    private ProductService productService;


    @GetMapping
    @ApiOperation(value = "产品列表", notes = "支持分页查询,字段模糊匹配,排序等")
    public ResponseResult<CommonPage<Product>> list(BasePageReqDTO param) {
        val result = productService.list(param);
        return ResponseResult.success(CommonPage.restPage(result));
    }


    @GetMapping("{id}")
    @ApiOperation(value = "产品详情", notes = "根据ID查询")
    public ResponseResult<Product> get(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
        val result = productService.get(id);
        return ResponseResult.success(result);
    }


    @PostMapping
    @ApiOperation(value = "添加产品", notes = "添加")
    public ResponseResult<Product> create(@Validated @RequestBody ProductCreateParam param) {
        val result = productService.create(param);
        return result != null ? ResponseResult.success(result) : ResponseResult.failed();
    }


    @ApiOperation(value = "删除产品", notes = "通过ID删除")
    @DeleteMapping(value = "{id}")
    public ResponseResult<Boolean> delete(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
        val result = productService.delete(id);
        return result ? ResponseResult.success(true) : ResponseResult.failed();
    }


    @ApiOperation(value = "修改产品", notes = "通过ID修改")
    @PutMapping(value = "{id}")
    public ResponseResult<Product> update(@PathVariable @ApiParam(value = "ID", required = true) Long id, @Validated @RequestBody ProductUpdateParam param) {
        val result = productService.update(id, param);
        return result != null ? ResponseResult.success(result) : ResponseResult.failed();
    }
}
